﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Msg.Api.Controllers
{
    /// <summary>
    /// 当前请求帮助类
    /// </summary>
    public class HttpContextHelper
    {
        /// <summary>
        ///   获取IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIpaddress()
        {
            string result = String.Empty;
            result = HttpContext.Current.Request.ServerVariables["HTTP_CDN_SRC_IP"];
            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.UserHostAddress;

            if (string.IsNullOrEmpty(result) || result.Split('.').Length != 4)
                return "127.0.0.1";

            return result;
        }
    }
}